package com.tianpengtech.front.mobile.bankno;

import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;
import com.tianpengtech.common.GlobalConfigConstant;
import com.tianpengtech.common.model.fairbankno.FairBankNo;
import com.tianpengtech.common.model.fairwithdraw.FairWithdraw;
import com.tianpengtech.front.mobile.common.util.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
 * Created by ggd on 2017/7/26 0026.
 */
public class MobileBanknoService {
    public static MobileBanknoService me = new MobileBanknoService();
    private static FairBankNo dao = FairBankNo.dao;
    private String domain=GlobalConfigConstant.DOMAIN;
//    private String domain="http://192.168.1.54:8090/assets/front/img/";
    /**
     * 保存银行卡信息
     * @param param
     * @return
     */
    public R save(Map<String,String> param) throws Exception{
        try{
            String wh_id=param.get("id");
            String bank=param.get("bank");
            String bank_no=param.get("bank_no");
            String real_name=param.get("real_name");
            String icon=param.get("icon");
            String branch=param.get("branch");
            if (isBankNoExists(bank_no,Long.parseLong(wh_id))){
                return R.fail("此银行卡已添加过");
            }
            String regex = "^[\\u4e00-\\u9fa5]*$";  
            Matcher m = Pattern.compile(regex).matcher(real_name);  
            if(!m.find()){
            	return R.fail("收款人姓名必须为中文");
            }
            if(StringUtils.isEmpty(branch)){
            	return R.fail("分行名称不能唯空");
            }
            FairBankNo fb = new FairBankNo();
            fb.set(FairBankNo.WH_ID,wh_id);
            fb.set(FairBankNo.BANK,bank);
            fb.set(FairBankNo.BANK_NO,bank_no);
            fb.set(FairBankNo.REAL_NAME,real_name);
            fb.set(FairBankNo.ICON,icon);
            fb.set(FairBankNo.BRANCH,branch);
            fb.save();
            R r=R.ok("成功");
            r.p("bankno",fb);
            CacheKit.remove(FairBankNo.CACHE_NAME,"bankno_"+wh_id);
            return r;
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    
    public R update(Map<String,String> param) throws Exception{
        try{
            String wh_id=param.get("wh_id");
            String id= param.get("id");
            String bank=param.get("bank");
            String bank_no=param.get("bank_no");
            String real_name=param.get("real_name");
            String icon=param.get("icon");
            String branch=param.get("branch");
//            if (isBankNoExists(bank_no,Long.parseLong(wh_id))){
//                return R.fail("此银行卡已添加过");
//            }
            String regex = "^[\\u4e00-\\u9fa5]*$";  
            Matcher m = Pattern.compile(regex).matcher(real_name);  
            if(!m.find()){
            	return R.fail("收款人姓名必须为中文");
            }
            if(StringUtils.isEmpty(branch)){
            	return R.fail("分行名称不能唯空");
            }
            FairBankNo fb = new FairBankNo();
            fb.set(FairBankNo.ID, id);
            fb.set(FairBankNo.WH_ID,wh_id);
            fb.set(FairBankNo.BANK,bank);
            fb.set(FairBankNo.BANK_NO,bank_no);
            fb.set(FairBankNo.REAL_NAME,real_name);
            fb.set(FairBankNo.ICON,icon);
            fb.set(FairBankNo.BRANCH,branch);
            fb.update();
            R r=R.ok("银行卡编辑保存成功");
            r.p("bankno",fb);
            CacheKit.remove(FairBankNo.CACHE_NAME,"bankno_"+wh_id);
            return r;
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    /**
     *  获取某个人得银行卡列表
     * @param id
     * @return
     */
    public R findBankno(long id){
        String sql = String.format("select %s,%s,%s,%s,icon,branch from %s where %s=? and %s=?",
        		FairBankNo.ID,FairBankNo.BANK,FairBankNo.REAL_NAME
        		,FairBankNo.BANK_NO,dao.tableName(),FairBankNo.DELETETAG,FairBankNo.WH_ID);
        List<FairBankNo> list=dao.find(sql,FairBankNo.DEL_NO,id);
        for (FairBankNo fairBankNo : list) {
        	fairBankNo.set(FairBankNo.ICON, domain+fairBankNo.getStr(FairBankNo.ICON));
		}
        R r = R.ok();
        r.p("banknos",list);
        return r;
    }

    /**
     * 判断用的银行卡是否添加过
     * @param bankNo
     * @param uid
     * @return
     */
    public boolean isBankNoExists(String bankNo,long uid){
        String sql = String.format("select %s from %s where %s=? and %s=? and %s=?",FairBankNo.ID,dao.tableName(),FairBankNo.DELETETAG,FairBankNo.WH_ID,FairBankNo.BANK_NO);
        FairBankNo no=dao.findFirst(sql,0,uid,bankNo);
        if (no==null){
            return false;
        }else{
            return true;
        }
    }


    /**
     * 判断用的银行卡是否添加过
     * @return
     */
    public boolean isBankNoExists(String bankId){
        String sql = String.format("select %s from %s where %s=? and %s=?",FairBankNo.ID,dao.tableName(),FairBankNo.DELETETAG,FairBankNo.ID);
        FairBankNo no=dao.findFirst(sql,0,bankId);
        if (no==null){
            return false;
        }else{
            return true;
        }
    }

    
    public R bankCategory(){
    	R r = R.ok();
    	List<Record>bankCategory = new ArrayList<Record>();
    	String[] banks = {"中国建设银行","招商银行","中国银行","中国工商银行","中国农业银行","浦发银行","华夏银行"};
    	String[] icons = {"jianshe.png","zhaoshang.png","zhongguo.png","gongshang.png"
    			,"nongye.png","pufa.png","huaxia.png"};
    	for (int i=0;i<banks.length;i++) {
    		Record record = new Record();
        	record.set("icon",domain+icons[i]);
        	record.set("name",banks[i]);
        	bankCategory.add(record);
		}
    	r.p("list", bankCategory);
    	return r;
    }
    
    
    
    public R unbindBankNo(int id ){
    	R r ;
    	//检查是否在当前银行卡上有相关提现记录信息
    	String sql="select * from "+FairWithdraw.dao.tableName()+ " where bank_id=? and status=? ";
    	FairWithdraw fairWithdraw = FairWithdraw.dao.findFirst(sql,id,FairWithdraw.STATUS_APPROVING);
    	
    	if(fairWithdraw!=null){
    		return R.fail("当前银行卡上未处理的提现信息，暂时不能进行当前操作");
    	}
    	
    	FairBankNo fairBankNo = FairBankNo.dao.findById(id);
    	fairBankNo.set(FairBankNo.DELETETAG, FairBankNo.DEL_YES);
    	boolean result = fairBankNo.update();
    	if(result){
    		r= R.ok("操作成功");
    	}else{
    		r= R.fail("解绑操作失败，当前记录不存在");
    	}
    	return r;
    }
}
